<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2019/1/15
  Time: 23:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品分类</title>
    <jsp:include page="${pageContext.request.contextPath}/common/tag.jsp"/>
</head>
    <script>
        $(function () {
            $("#fltable").bootstrapTable({
                url : '${pageContext.request.contextPath}/fenlei/queryfenlei',
                toolbar:'#add',
                columns:[
                    {checkbox:true},
                    {field:'fid',title:'编号',align:'center'},
                    {field:'fname',title:'名称',align:'center'},
                    {title:'操作',align:'center',
                        formatter:function (value,row,index) {
                            return  "<button class=\"btn btn-primary\" id=\"edits\" data-toggle=\"modal\" href=\"#addmodal\" onclick='huoqu("+index+")'><span class=\"glyphicon glyphicon-edit\"></span>修改</button>&nbsp;&nbsp;<button class=\"btn btn-primary\" onclick='shanchu("+row.fid+")'><span class=\"glyphicon glyphicon-trash\"></span>&nbsp;&nbsp;删除</button>";
                        }
                    }
                ]
            })

            $("#add").click(function () {
                $("#jiaxiu").val(1);
                $("#id").val(0)
            })
           /* $("#add").val(1);*/
        })
    </script>
<body style="margin: 20px 20px;overflow-y: hidden">
    <div>
        <button id="add" class="btn btn-primary" data-toggle="modal" href="#addmodal">添加</button>
        <table id="fltable"></table>

        <div class="modal fade" id="addmodal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">添加分类</h4>
                    </div>
                    <div class="modal-body">
                        <form id="addform" action="" method="post" role="form">
                            <input type="hidden" id="jiaxiu" value="1">
                            <div class="form-group">
                                <label for="">分类名称</label>
                                <input type="text" id="fname" class="form-control">
                            </div>
                            <input type="hidden" id="id">
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" onclick="baocun()">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
    </div>
</body>
<script>

    function baocun() {
        var jiaxiu = $("#jiaxiu").val();
        if(jiaxiu==1){
            /*alert("添加")*/
            var name = $("#fname").val();
            if(name==''){
                alert("不能为空");
                return false;
            }
            $.post('${pageContext.request.contextPath}/fenlei/insert',{"name":name},function (data) {
                $("#addmodal").modal('hide');
                $("#fltable").bootstrapTable('load',data);
            })
        }else{
            var name = $("#fname").val();
            if(name==''){
                alert("不能为空");
                return false;
            }
            var id = $("#id").val();
            $.post('${pageContext.request.contextPath}/fenlei/update',{"name":name,"id":id},function (data) {
                $("#addmodal").modal('hide');
                $("#fltable").bootstrapTable('load',data);
            })
        }
    }

    function huoqu(index) {
        $("#fltable").bootstrapTable("uncheckAll");
        $("#fltable").bootstrapTable("check",index);
        var rows = $("#fltable").bootstrapTable('getSelections')[0];
        console.log(rows);
        $("#id").val(rows.fid);
        $("#jiaxiu").val(2);
        $("#fname").val(rows.fname);
    }

    function shanchu(id) {
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false
        },function () {
            $.ajax({
                type:'post',
                url: '${pageContext.request.contextPath}/fenlei/delete',
                data : {"id":id},
                success:function (data) {
                    //从新绑定表格
                    $("#fltable").bootstrapTable('load',data);
                    //提示增加成功
                    swal("提示信息", "批量删除成功!", "success");
                }
            });
        });
    }
</script>
</html>
